from tile import *
import random
import os
from sfx import *

basic_path = os.path.dirname(os.path.realpath('main.py'))
data_path = basic_path+os.sep+'DATA'+os.sep
sfx = sfx(data_path)

class monster():
	
	def __init__(self,x,y,element=0):
		
		self.element = element
		
		if element == 0:
			self.graphic = 'fire'
		elif element == 1:
			self.graphic = 'water'
		elif element == 2:
			self.graphic = 'air'
		else:
			self.graphic = 'earth'
			
		self.lp = 4
		
		self.x = x
		self.y = y
		
		self.x_dest = x
		self.y_dest = y
		
		self.move_blocked = False	
	
	def fight(self,player,te_maP):
		
		blink = 'none'
		
		self.lp -= 1
		
		if player.mage == 0:
			if self.element == 1:
				player.lp -= 3
				sfx.play_sound('hit')
				blink = 'red'
			elif self.element == 0:
				blink = 'blue'
			elif self.element == 2:
				player.lp -= 1
				sfx.play_sound('hit')
				blink = 'red'
			else:
				sfx.play_sound('dead')
				self.lp = 0
		elif player.mage == 1:
			if self.element == 2:
				player.lp -= 3
				sfx.play_sound('hit')
				blink = 'red'
			elif self.element == 1:
				blink = 'blue'
			elif self.element == 3:
				player.lp -= 1
				sfx.play_sound('hit')
				blink = 'red'
			else:
				sfx.play_sound('dead')
				self.lp = 0
		elif player.mage == 2:
			if self.element == 3:
				player.lp -= 3
				sfx.play_sound('hit')
				blink = 'red'
			elif self.element == 2:
				blink = 'blue'
			elif self.element == 0:
				player.lp -= 1
				sfx.play_sound('hit')
				blink = 'red'
			else:
				sfx.play_sound('dead')
				self.lp = 0
		if player.mage == 3:
			if self.element == 0:
				player.lp -= 3
				sfx.play_sound('hit')
				blink = 'red'
			elif self.element == 3:
				blink = 'blue'
			elif self.element == 1:
				player.lp -= 1
				sfx.play_sound('hit')
				blink = 'red'
			else:
				sfx.play_sound('dead')
				self.lp = 0
				
		###knockback
		
		dest_check = False
		
		if self.x >= player.x and self.y-player.y == 0:
			dest_check = True
			if self.check_wall_col(te_maP,self.x+1,self.y,player):
				self.x_dest = self.x + 1
			if player.check_wall_col(te_maP,player.x-1,player.y):
				player.x_dest = player.x - 1
		elif self.x <= player.x and self.y-player.y == 0:
			dest_check = True
			if self.check_wall_col(te_maP,self.x-1,self.y,player):
				self.x_dest = self.x - 1
			if player.check_wall_col(te_maP,player.x+1,player.y):
				player.x_dest = player.x + 1
				
		if dest_check == False:
			if self.y >= player.y and self.x-player.x == 0:
				if self.check_wall_col(te_maP,self.x,self.y+1,player):
					self.y_dest =  self.y + 1
				if player.check_wall_col(te_maP,player.x,player.y-1):
					player.y_dest = player.y - 1
			elif self.y <= player.y and self.x-player.x == 0:
				if self.check_wall_col(te_maP,self.x,self.y-1,player):
					self.y_dest =  self.y -1
				if player.check_wall_col(te_maP,player.x,player.y+1):
					player.y_dest = player.y + 1
		
		if self.x_dest != self.x or self.y_dest != self.y or player.x_dest != player.x or player.y_dest != player.y:
			self.move_blocked = True
			player.move_blocked = True
	
		return blink
	
	def check_wall_col(self,te_maP,x,y,player):
		
		if x != self.x:
			x_change = True
		else:
			x_change = False
		
		if x_change == True and self.y_dest != self.y:
			self.y_dest = self.y
		elif x_change == False and self.x_dest != self.x:
			self.x_dest = self.x
		
		if self.move_blocked == True:
			return False
		
		if (min(x,y) > -1 and max(x,y) < 8) and (x != player.x or y != player.y):	
			return tile_list[te_maP.tiles[x][y]].move
		else:
			return False
	
	def AI_move(self,player,te_maP):
		
		#self.move_blocked = False
		try:
			self.x = self.x_dest
			self.y = self.y_dest
		
			x_dist = player.x - self.x
			y_dist = player.y - self.y
		
			x_dist_c = x_dist ** 2
			y_dist_c = y_dist ** 2
		
			if max(x_dist_c,y_dist_c) == 1 and min(x_dist_c,y_dist_c) == 0:
				if player.move_blocked == False:
					blink = self.fight(player,te_maP)
					return blink
			
			else:
				possible_dir = []
			
				if x_dist <= 0:
					possible_dir.append((-1,0))
				else:
					possible_dir.append((1,0))
				
				if y_dist <= 0:
					possible_dir.append((0,-1))
				else:
					possible_dir.append((0,1))	
			
				
				free_dir = []
				for i in range(0,len(possible_dir)):
					if tile_list[te_maP.tiles[self.x+possible_dir[i][0]][self.y+possible_dir[i][1]]].move:
						free_dir.append(possible_dir[i])
						if len(free_dir) == 0:
							return False
			
				if len(free_dir) == 1:
					final_move = free_dir[0]
				else:
					ran = random.randint(0,len(free_dir)-1)
					final_move = free_dir[ran]
				
				for m in te_maP.monsters:
					if (self.x+final_move[0] == m.x and self.y+final_move[1] == m.y) or (self.x+final_move[0] == m.x_dest and self.y+final_move[1] == m.y_dest):
						return False
			
				if (self.x + final_move[0] != player.x and self.x + final_move[0] != player.x_dest) or (self.y + final_move[1] != player.y and self.y + final_move[1] != player.y_dest):		
					self.x_dest = self.x+final_move[0]
					self.y_dest = self.y+final_move[1]
				else:
					return False
		except:
			return False
